<?php require_once('../Connections/locale.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO numeri (id_rivista, identificativo_numero, `data`, descrizione) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['id_rivista'], "int"),
                       GetSQLValueString($_POST['identificativo_numero'], "int"),
                       GetSQLValueString($_POST['data'], "date"),
                       GetSQLValueString($_POST['descrizione'], "text"));

  mysql_select_db($database_locale, $locale);
  $Result1 = mysql_query($insertSQL, $locale) or die(mysql_error());
  //Inserimento Abbonati
  //$insertSQL = "insert into numeri_abbonati(id_abbonato, id_rivista, id_numero) select abbonati.id_abbonato,".$_POST['id_rivista'].",(SELECT MAX(id_numero) FROM `numeri` where id_rivista=".$_POST['id_rivista'].") as IDNUMERO   from abbonati_riviste, abbonati where da_numero <= ".$_POST['identificativo_numero']." and a_numero+(select numeri_extra from categorie where id_categoria = abbonati.id_categoria) >=".$_POST['identificativo_numero']." and id_rivista=".$_POST['id_rivista']." and abbonati.id_abbonato = abbonati_riviste.id_abbonato";
  $insertSQL = "insert into numeri_abbonati(id_abbonato, id_rivista, id_numero) select abbonati.id_abbonato,".$_POST['id_rivista'].",(SELECT MAX(id_numero) FROM `numeri` where id_rivista=".$_POST['id_rivista'].") as IDNUMERO   from abbonati_riviste, abbonati where da_numero <= ".$_POST['identificativo_numero']." and a_numero+(select numeri_extra from categorie where id_categoria = abbonati.id_categoria) >=".$_POST['identificativo_numero']." and id_rivista=".$_POST['id_rivista']." and abbonati.id_abbonato = abbonati_riviste.id_abbonato and year(abbonati_riviste.data_decorrenza) <= year('".$_POST['data']."') and year(abbonati_riviste.data_scadenza) >= year('".$_POST['data']."')";
  //mysql_select_db($database_locale, $locale);
  echo $insertSQL;
  $Result1 = mysql_query($insertSQL, $locale) or die(mysql_error());


  $insertGoTo = "elenco_numeri.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_locale, $locale);
$query_riviste = "SELECT * FROM riviste";
$riviste = mysql_query($query_riviste, $locale) or die(mysql_error());
$row_riviste = mysql_fetch_assoc($riviste);
$totalRows_riviste = mysql_num_rows($riviste);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../style.css" rel="stylesheet" type="text/css">.
<link rel="stylesheet" type="text/css" href="../epoch_styles.css" />
<script type="text/javascript" src="../jscripts/epoch_classes.js"></script>
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right">Rivista:</td>
      <td>
        <select name="id_rivista">
          <?php 
do {  
?>
          <option value="<?php echo $row_riviste['id_rivista']?>" ><?php echo $row_riviste['descrizione']?></option>
          <?php
} while ($row_riviste = mysql_fetch_assoc($riviste));
?>
        </select>
      </td>
    <tr>
    <tr valign="baseline">
      <td nowrap align="right">Identificativo:</td>
      <td><input type="text" name="identificativo_numero" value="" size="5"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Data:</td>
      <td><input type="text" name="data" id="data" value="" size="11">
	  	  <script language="JavaScript" type="text/javascript">
		var dp_cal  = new Epoch('epoch_popup','popup',document.getElementById('data'));
	</script>
	  
	  </td>
    </tr>
    <tr valign="baseline">
      <td align="right" valign="top" nowrap>Descrizione:</td>
      <td><textarea name="descrizione" cols="32"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Inserisci Numero"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($riviste);
?>
